﻿namespace YnabCsvConverter.Core.Converters
{
   using System;
   using System.Collections.Generic;
   using System.Globalization;
   using System.Linq;
   using Betterment;

   /// <summary>
   /// Betterment Converter
   /// (see: http://ynabcsvconverter.codeplex.com/discussions/428605)
   /// 
   /// Thanks to friedrickjoel for this converter.
   /// </summary>
   [ConverterName("Betterment")]
   [ConverterAuthor("friedrickjoel")]
   public class BettermentConverter : BaseConverter, IConverter
   {
      /// <summary>
      /// Initializes a new instance of the <see cref="BettermentConverter" /> class.
      /// </summary>
      public BettermentConverter()
      {
         // The source cvs uses a 'us' culture formatted file.
         SourceCulture = CultureInfo.GetCultureInfo("EN-US");
      }

      /// <summary>
      /// Converts this instance.
      /// </summary>
      /// <param name="sourceFilename">The source filename.</param>
      /// <param name="targetFilename">The target filename.</param>
      /// <param name="mode">The creation mode.</param>
      /// <returns>
      /// The Converter Result.
      /// </returns>
      public ConverterResult Convert(string sourceFilename, string targetFilename, CreationMode mode)
      {
         // Read specific records from source file.
         IEnumerable<BettermentRecord> bettermentRecords = ReadRecords<BettermentRecord>(sourceFilename, ',', true);

         // Convert records from source file to YNAB records.
         IEnumerable<YnabRecord> ynabRecords = ConvertRecords(bettermentRecords);

         // Write YNAB records to target file.
         WriteRecords(ynabRecords, targetFilename, mode);

         // Return a result
         return new ConverterResult { IsSuccess = true };
      }

      /// <summary>
      /// Converts to ynab lines.
      /// </summary>
      /// <param name="bettermentRecords">The betterment records.</param>
      /// <returns></returns>
      /// <exception cref="System.NotImplementedException"></exception>
      public IEnumerable<YnabRecord> ConvertRecords(IEnumerable<BettermentRecord> bettermentRecords)
      {
         return bettermentRecords.Select(ConvertRecord).ToList();
      }

      /// <summary>
      /// Converts to ynab CSV line.
      /// </summary>
      /// <param name="bettermentRecord">The betterment CSV line.</param>
      /// <returns></returns>
      /// <exception cref="System.NotImplementedException"></exception>
      public YnabRecord ConvertRecord(BettermentRecord bettermentRecord)
      {
         var ynabRecord = new YnabRecord();

         ynabRecord.SetDate(bettermentRecord.DateTime, SourceCulture);

         ynabRecord.SetInflowOutflow(StringCurrenyToDecimal(bettermentRecord.Amount));
         ynabRecord.Category = bettermentRecord.Description;
         ynabRecord.Payee = bettermentRecord.AccountName;
         ynabRecord.Memo = bettermentRecord.Description;         
         
         return ynabRecord;
      }

      /// <summary>
      /// Strings the curreny to decimal.
      /// </summary>
      /// <param name="value">The value.</param>
      /// <returns></returns>
      public decimal StringCurrenyToDecimal(string value)
      {
          decimal parsedValue = 0;
          
          if (value.Length > 0)
          {
             // Clear spaces.
             value = value.Replace(" ", "");

             parsedValue = Decimal.Parse(value,
                                       NumberStyles.Any | NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands |
                                       NumberStyles.AllowDecimalPoint, SourceCulture.NumberFormat);
          }

         return parsedValue;
      }
   }
}
